package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // controller 注解
@Api(tags = "分类相关接口")
@RequestMapping(value = "/admin/category")
@Slf4j // 日志注解
public class CategoryController {
    // 注入Service层Bean对象
    @Autowired
    private CategoryService categoryService;

    /*新增分类接口*/
    @PostMapping
    @ApiOperation(value = "新增分类方法")
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类：{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /*分页查询分类接口*/
    @GetMapping(value = "/page")
    @ApiOperation(value = "分类分页查询方法")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分页查询分类,参数{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /*删除分类接口
        根据id删除分类,不过是传递id,不是路径参数
    */
    @DeleteMapping
    @ApiOperation(value = "删除分类方法")
    public Result deleteById(Long id) {
        log.info("删除分类{}", id);
        // 调用Service层删除分类方法
        categoryService.deleteById(id);
        return Result.success();
    }

    /*
     * 修改分类方法
     * */
    @PutMapping(value = "")
    @ApiOperation(value = "修改分类方法")
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        log.info("修改分类信息方法，修改参数为{}", categoryDTO);
        // 调用service方法
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /*删除分类方法*/
    @PostMapping(value = "/status/{status}")
    @ApiOperation(value = "启用或者禁用分类")
    public Result enableOrDisable(@PathVariable("status") Integer status, Long id) {
        /*记录日志*/
        log.info("修改分类状态，参数是{}，{}", status, id);
        categoryService.enableOrDisable(status, id);
        return Result.success();
    }

    /*根据类型查询分类
    * 比如 根据套餐分类或者根据菜品分类
    *
    * */
    @GetMapping(value = "/list")
    @ApiOperation(value = "根据类型查询分类方法")
    public Result<List<Category>> list(Integer type) {
        /*记录日志*/
        log.info("根据类型查询分类，{}", type);
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
